/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SWANLINK_KEYBOARD_SRV_CLIENT_H
#define SWANLINK_KEYBOARD_SRV_CLIENT_H

#include <singleton.h>
#include <memory>
#include <mutex>
#include "iremote_object.h"
#include "ikeyboard_srv.h"
#include "cashdevice_client.h"
#include "icashdevice_srv.h"

namespace SwanLink {
namespace Finance {
using namespace OHOS;
class KeyboardSrvClient final : public DelayedRefSingleton<KeyboardSrvClient>, public CashDeviceClient {
    DECLARE_DELAYED_REF_SINGLETON(KeyboardSrvClient)

public:
    DISALLOW_COPY_AND_MOVE(KeyboardSrvClient);


    /**
     * 是否使用国密
     */
    bool IsSM4Enabled(int handle);
    /**
     * 获取密码键盘序列号
     */
    int GetSN(int handle, char sn[], int &len);
    /**
     * 计算MAC
     */
    int CalculateMAC(int handle, const KeyInfo& key, char plain[], int len, char enc_data[], int &enc_len);
    /**
     * 加密数据
     */
    int EncryptData(int handle, const KeyInfo& key, char plain[], int len, char enc_data[], int &len_enc);
    /**
     * 设置PIN的长度限制
     */
    int SetPINLength(int handle, int minLen, int maxLen);
    /**
     * 设置明文密文模式
     */
    int SetTextMode(int handle, TextMode mode);
    /**
     * 清空文本
     */
    void ClearTextMode(int handle);
    /**
     * 加载主密钥
     */
    int LoadMasterKey(int handle, KeyID masterKeyID, unsigned char *KeyValue);
    /**
     * 加载用户密钥
     */
    int LoadUserKey(int handle, KeyID masterKeyID, userKeyType user_Key_Type, unsigned char *KeyValue);
    /**
     * 更新密钥
     */
    int UpdateMasterKey(int handle, KeyID masterKeyID, unsigned char *KeyValue);
    /**
     * 更新用户密钥
     */
    int UpdateUserKey(int handle, KeyID masterKeyID, userKeyType user_Key_Type, unsigned char *KeyValue);
    /**
     * 删除用户密钥
     */
    int DeleteUserKey(int handle, KeyID masterKeyID, userKeyType user_Key_Type);
    /**
     * 监听
     */
    void ListenForPin(int handle, PinListener* listener, int timeout);

    void ListenLoop(int handle);

 private:
    class KeyboardSrvDeathRecipient : public IRemoteObject::DeathRecipient {
     public:
        KeyboardSrvDeathRecipient() = default;
        ~KeyboardSrvDeathRecipient() = default;
        void OnRemoteDied(const wptr<IRemoteObject>& remote);
     private:
        DISALLOW_COPY_AND_MOVE(KeyboardSrvDeathRecipient);
    };
    ErrCode Connect();
    sptr<ICashDeviceSrv> Proxy() {
        return proxy_;
    }
    sptr<IKeyboardSrv> proxy_ {nullptr};
    sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr};
    void ResetProxy(const wptr<IRemoteObject>& remote);
    std::mutex mutex_;
};
} // namespace Finance
} // namespace SwanLink

#endif // SWANLINK_KEYBOARD_SRV_CLIENT_H
